package com.example.common.controller;

import com.example.common.mapper.IAdminMapper;
import com.example.common.pojo.Admin;
import com.example.common.pojo.RightList;
import com.example.common.service.IAdminService;
import com.example.common.service.IGrantService;
import com.example.right.service.IRightService;
import com.example.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/grant")
public class GrantController {
    private final IRightService iRightService;
    private final IAdminService iAdminService;
    private final IGrantService iGrantService;
    @Autowired
    public GrantController(IRightService iRightService,IAdminService iAdminService,IGrantService iGrantService){
        this.iRightService=iRightService;
        this.iAdminService=iAdminService;
        this.iGrantService=iGrantService;
    }
    @PostMapping("/{id1}/{id2}")
    @ResponseStatus(HttpStatus.OK)
    public int grant(@PathVariable("id1")Integer id1, @PathVariable("id2") String id2, @RequestBody RightList rightList){
        if(!this.iRightService.checkPermission(id1,Constants.INSERT)){
            return Constants.NO_PERMISSION;
        }else{
            return this.iGrantService.insertIntoUserRight(id1,findOneAdmin(id1,id2).getMemberId(),rightList);
        }

    }

    @RequestMapping("/find/{id1}/{id2}")
    public Admin findOneAdmin(@PathVariable("id1")Integer id1,@PathVariable("id2")String id2){
        return this.iAdminService.findAdminByIdentify(id2);
    }
}
